/*
 * Copyright (C) 2019 ~ 2021 Deepin Technology Co., Ltd.
 *
 * Author:     caitao <caitao@uniontech.com>
 *
 * Maintainer: caitao <caitao@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "dlistviewmulticoldelegate.h"
#include "dlistviewmulticolcommon.h"

#include <QPainter>
#include <DApplicationHelper>
#include <QAbstractItemView>
#include <QIcon>
#include <QPainterPath>

DWIDGET_USE_NAMESPACE

DListViewMultiColDelegate::DListViewMultiColDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}

void DListViewMultiColDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    const QAbstractItemModel *ItemModel = index.model();

    QStyleOptionViewItem styleOption = option;

    // 画背景
    // 判断奇偶
    bool drawBackground = ((index.row() & 1) == 1);
    if (drawBackground) {
        QPainterPath path;
        path.addRoundedRect(styleOption.rect, 8, 8);
        painter->save();
        painter->setOpacity(1);
        painter->setRenderHint(QPainter::Antialiasing);

        DPalette pa = DApplicationHelper::instance()->palette(styleOption.widget);
        QBrush backgroundBrush = pa.brush(DPalette::ColorType::ItemBackground);

        painter->fillPath(path, backgroundBrush);
        painter->restore();
    }

    // 画当鼠标悬浮此处时的背景
    bool isHovering = (QStyle::StateFlag::State_MouseOver & option.state);
    if (isHovering) {
        QPainterPath path;
        path.addRoundedRect(styleOption.rect, 8, 8);
        painter->save();
        painter->setOpacity(1);
        painter->setRenderHint(QPainter::Antialiasing);

        DPalette pa = DApplicationHelper::instance()->palette(styleOption.widget);
        QBrush backgroundBrush = pa.brush(DPalette::ColorType::ObviousBackground);

        painter->fillPath(path, backgroundBrush);
        painter->restore();
    }

    // 画当被选中时的背景
    bool isSelection = (QStyle::StateFlag::State_Selected & option.state);
    if (isSelection) {
        QPainterPath path;
        path.addRoundedRect(styleOption.rect, 8, 8);
        painter->save();
        painter->setOpacity(1);
        painter->setRenderHint(QPainter::Antialiasing);

        DPalette pa = DApplicationHelper::instance()->palette(styleOption.widget);
        QBrush backgroundBrush = pa.brush(DPalette::ColorRole::Highlight);

        painter->fillPath(path, backgroundBrush);
        painter->restore();
    }

    // 画本行各列内容
    int leftColWidth = 0;
    QFontMetrics fontMetrics = styleOption.fontMetrics;
    QList<int> headerWidthList = ItemModel->property(PROPERTY_KEY_HEADER_SECTION_WIDTH_LIST).value<QList<int>>();
    for (int i = 0; i < headerWidthList.size(); ++i) {
        int thisColWidth = headerWidthList[i];

        // 获取列数据
        QModelIndex colIndex = ItemModel->index(index.row(), i);

        // icon
        painter->save();
        QIcon icon;
        int iconWidth = 0;
        QVariant decorationVar = ItemModel->data(colIndex, Qt::ItemDataRole::DecorationRole);
        bool haveIcon = (QVariant::Type::Icon == decorationVar.type());
        if (haveIcon) {
            icon = ItemModel->data(colIndex, Qt::ItemDataRole::DecorationRole).value<QIcon>();
            iconWidth = styleOption.rect.height();
            QRect iconRect(10 + styleOption.rect.x() + leftColWidth, styleOption.rect.y(), iconWidth, styleOption.rect.height());
            icon.paint(painter, iconRect);
        }
        painter->restore();

        // 画文字
        // 文字颜色
        painter->save();
        QVariant colorVar = ItemModel->data(colIndex, Qt::ItemDataRole::TextColorRole);
        if (QVariant::Type::Color == colorVar.type()) {
            painter->setPen(colorVar.value<QColor>());
        }

        QRect textRect(10 + styleOption.rect.x() + leftColWidth + iconWidth, styleOption.rect.y(), thisColWidth - iconWidth, styleOption.rect.height());
        QString displayText = ItemModel->data(colIndex, Qt::ItemDataRole::DisplayRole).toString();
        QString elidedDisplayText = fontMetrics.elidedText(displayText, Qt::TextElideMode::ElideMiddle, thisColWidth - iconWidth);
        painter->drawText(textRect, elidedDisplayText);
        leftColWidth += thisColWidth;
        painter->restore();
    }
}

QSize DListViewMultiColDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QStyledItemDelegate::sizeHint(option, index);
}

QWidget *DListViewMultiColDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QStyledItemDelegate::createEditor(parent, option, index);
}

void DListViewMultiColDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyledItemDelegate::updateEditorGeometry(editor, option, index);
}

void DListViewMultiColDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QStyledItemDelegate::setEditorData(editor, index);
}
